<?php
namespace app\index\controller;
use think\Controller;
use think\Request;
use think\Db;
use Orders;
date_default_timezone_set('PRC');
class Index extends Controller
{   
    
   /* public function _initialize(){
         $open = db('set')->where('id',1)->find();
       if($open['status'] == 1){
            $this->redirect('index/index/index');
       }else{
            $this->error('网站正在抢修');
       }
    }*/
    //首页开奖结果展示
     public function index(){
      $open = db('set')->where('id',1)->find();
      // print_r($open);die;
        if($open['status'] == 1){
            $win=Db::table('ma_order')->alias('a')->join('ma_users b','a.uid = b.uid')->field('a.uid,b.username,SUM(a.winmoney) as winmoneys')->group('uid')->whereTime('wintime','today')->limit(11)->order('winmoneys desc')->select();
            $data = db('result')->limit(1,100)->order('id desc')->select();
            //最新一期开奖结果
            $count = db('result')->count();
            if(!$count){
                $this->redirect('index/status/index');
            }else{
            $new = db('result')->limit(1,1)->order('id desc')->select();
            if($new){
                $new3 = $new[0];//当前期数  开奖数据
                //$next_qishu = $new[0]['qishu']+1; //下一期
                //$next_time  = $new[0]['qishu']+1; //下一期
                $new2[] = $new[0]['ball1'];
                $new2[] = $new[0]['ball2'];
                $new2[] = $new[0]['ball3'];
                $new2[] = $new[0]['ball4'];
                $new2[] = $new[0]['ball5'];
                $new2[] = $new[0]['ball6'];
                $new2[] = $new[0]['ball7'];
                $new2[] = $new[0]['ball8'];
                $new2[] = $new[0]['ball9'];
                $new2[] = $new[0]['ball10'];
            }else{
                $new3 = "";
                $new2 = "";
            }
            //获取最新开奖数据 
            $next_time = $this->nexts();//下一期封盘的时间
            
            //print_r($next_time);die;
            //用户的金额
            $uid = session('uid');
            $money = db('users')->field('uid,username,money,vip')->where('uid',$uid)->find();
            $todays = db('order')->field('SUM(money) as moneys')->where('uid',$uid)->whereTime('ordertime','today')->select();
            $today = db('order')->field('SUM(winmoney) as moneys')->group('uid')->where('uid',$uid)->where('status',1)->whereTime('wintime','today')->select();
            if($todays == null){
            }elseif($today== null){
                 $today = round((0-$todays[0]['moneys']),2);
            }else{
                $today = $today[0]['moneys'];
                $todays = $todays[0]['moneys'];
                $today = round(($today - $todays),2);
            }
            /*$newtime = date('Y/m/d H:i:s');*/
            $newtime =time();
            /*dump(date('Y/m/d H:i:s'));*/
            //公告显示
            $notice = db('notice')->limit(1)->order('id desc')->select();
            $this->assign('money',$money);
            $this->assign('today',$today);
            $this->assign('new2',$new2);
            $this->assign('new',$new3);
            $this->assign('newtime',$newtime);
            $this->assign('kaij_time',$next_time['kaij_time']);
            $this->assign('feng_time',$next_time['feng_time']);

            $this->assign('next_qishu',$next_time['qishu']);
            //$this->assign('next_qishu',$next_qishu);
            $this->assign('data',$data);
            $this->assign('win',$win);
            $this->assign('set',$open['allodds']);
            $this->assign('notice',$notice);
            }
            return $this->fetch();
        }else{
          $this->redirect('index/status/index');
       }
    }

    //下一期开奖时间
    public function nexts(){
        $data = db('result')->field('id,qishu,time')->limit(2)->order('id desc')->select();
        $datas['qishu'] = $data[0]['qishu'];
        $time = strtotime($data[1]['time']);
        $datas['feng_times'] = strtotime($data[0]['time'])-120;
        $datas['feng_time'] = date("Y/m/d H:i:s",$datas['feng_times']);
        $timed = strtotime($data[0]['time']);
        $datas['kaij_time'] = date('Y/m/d H:i:s',$timed);
        return $datas;
    }    

       public function data($qishu=""){
         if(request()->isPost()){
            $qishu = input('post.qishu');
            $res = db('result')->where('qishu',$qishu)->find();
            $times = $res['time'];
            $timedd = strtotime($times)+3600;

            $opens = db('set')->field('return')->where('id',1)->find();
            /*dump($opens['return']);*/
            $timesd = date('Y-m-d',$timedd).' 09:07:'.$opens['return'];

            $timesH = date('H',strtotime($res['time']));
            $timesi = date('i',strtotime($res['time']));
            if($timesH == 23  &&  $timesi >= 55){
                $da['time'] = $timesd;
            }else{
                $time = strtotime($res['time'])+300;
                $da['time'] = date('Y-m-d H:i:s',$time);
            }
            $da['qishu'] =$res['qishu']+1;
            $qi = db('result')->where('qishu',$da['qishu'])->select();
            if(!$qi && !empty($qishu)){
               $qs = db('result')->insertGetId($da);
               if($qs){
                  return 200;
               }else{
                  return 201;
               }
            }else{
                echo '<br/>';
                 print_r($da['qishu']);
                return 201;
            }
         }else{
            $res = db('result')->where('qishu',$qishu)->find();
            print_r($res['qishu']);
            $times = $res['time'];
            $timedd = strtotime($times)+3600;
            $timesd = date('Y-m-d',$timedd).' 09:07:15';
            $timesH = date('H',strtotime($res['time']));
            $timesi = date('i',strtotime($res['time']));
            if($timesH == 23  &&  $timesi >= 55){
                $da['time'] = $timesd;
            }else{
                $time = strtotime($res['time'])+300;
                $da['time'] = date('Y-m-d H:i:s',$time);
            }

            $da['qishu'] =$res['qishu']+1;
            
            $qi = db('result')->where('qishu',$da['qishu'])->select();

            if(empty($qi) && !empty($qishu)){

               $qs = db('result')->insertGetId($da);

               if($qs){
                  return 200;
               }else{
                  return 201;
               }

            }else{
                echo '<br/>';
                print_r($da['qishu']);
                return 201;
            }


            // return 201;
         }   
         
       }
        
       //  public function data($qishu){
            
       //      // $qishu = input('post.qishu');
       //      //$data_result = db('result')->field('id,qishu,time')->limit(1)->order('id desc')->find();
       //      // $shang_qishu = 
       //      // if($data_result['qishu'] == $qishu){
       //      // }    
       //      $res = db('result')->where('qishu',$qishu)->find();
       //      print_r($res['qishu']);
       //      $times = $res['time'];
       //      $timedd = strtotime($times)+3600;
       //      $timesd = date('Y-m-d',$timedd).' 09:07:15';
       //      $timesH = date('H',strtotime($res['time']));
       //      $timesi = date('i',strtotime($res['time']));
       //      if($timesH == 23  &&  $timesi >= 55){
       //          $da['time'] = $timesd;
       //      }else{
       //          $time = strtotime($res['time'])+300;
       //          $da['time'] = date('Y-m-d H:i:s',$time);
       //      }
       //      $da['qishu'] =$res['qishu']+1;
            
       //      $qi = db('result')->where('qishu',$da['qishu'])->select();

       //      if(empty($qi) && !empty($qishu)){

       //         $qs = db('result')->insertGetId($da);

       //         if($qs){
       //            return 200;
       //         }else{
       //            return 201;
       //         }

       //      }else{
       //          return 201;
       //      }
       // }



    //插入开奖结果数据
    public function datas(){
        //echo 222;die;
   		//$msg = file_get_contents("http://f.apiplus.net/bjpk10-1.json"); 
        //$msg=array();
        date_default_timezone_set('PRC');
		$msg = file_get_contents("http://api.caipiaokong.cn/lottery/?name=bjpks&format=json&uid=998135&token=0d42dee99095bdc942a1bf0d20ef0d0de76071a4");
        //print_r($msg);die;
        $data = json_decode($msg,true);
        foreach ($data as $key => $value) {
            $datas[]['expect'] = $key;
            $datas[] = $value;
        }

        $datamsg['qishu'] = $datas[0]['expect'];
        if(!empty($datas[1]['dateline']) && (!empty($datas[1]['number']))){
            /*$datamsg['time'] = $datas[1]['dateline'];*/
            $code = explode(",",$datas[1]['number']);
            $datamsg['ball1'] = $code[0];
            $datamsg['ball2'] = $code[1];
            $datamsg['ball3'] = $code[2];
            $datamsg['ball4'] = $code[3];
            $datamsg['ball5'] = $code[4];
            $datamsg['ball6'] = $code[5];
            $datamsg['ball7'] = $code[6];
            $datamsg['ball8'] = $code[7];
            $datamsg['ball9'] = $code[8];
            $datamsg['ball10'] = $code[9];
            if($datamsg['ball1']<6){
               $datamsg['page'] = '跌'; 
               $datamsg['status'] = 0; 
            }
            if($datamsg['ball1']>5){
               $datamsg['page'] = '涨'; 
               $datamsg['status'] = 1; 
            }
            $msg = db('result')->field('qishu')->count();
            if(!$msg){
                $datamsg['time'] = $datas[1]['dateline'];
                $id =db('result')->insertGetId($datamsg);
                $next = db('result')->where('id',$id)->find();
                $time = strtotime($next['time'])+300;
                $da['qishu'] = $next['qishu']+1;
                $da['time'] = date('Y-m-d H:i:s',$time);
                $ne = db('result')->insertGetId($da);
            }

            $next_time = $this->data($datamsg['qishu']); 

            $qi = db('result')->where('qishu',$datamsg['qishu'])->find();
            if(empty($qi['ball1'])){
                    $qishu = db('result')->where('qishu',$datamsg['qishu'])->update($datamsg);
            }
            $code = controller('Orders', 'controller');
            $win = $code->wins(); 
            return $win; 
        }
    }  
    
    //插入开奖结果数据
    public function datas2(){
        //echo 222;die;
        //$msg = file_get_contents("http://f.apiplus.net/bjpk10-1.json"); 
        //$msg=array();
        date_default_timezone_set('PRC');
        $msg = file_get_contents("http://api.caipiaokong.cn/lottery/?name=bjpks&format=json&uid=1022858&token=a1a9033d7bc6e2a9cf72e4f2d134fd7fb9bd42a4");
        //print_r($msg);die;
        $data = json_decode($msg,true);
        foreach ($data as $key => $value) {
            $datas[]['expect'] = $key;
            $datas[] = $value;
        }

        $datamsg['qishu'] = $datas[0]['expect'];
        if(!empty($datas[1]['dateline']) && (!empty($datas[1]['number']))){
            /*$datamsg['time'] = $datas[1]['dateline'];*/
            $code = explode(",",$datas[1]['number']);
            $datamsg['ball1'] = $code[0];
            $datamsg['ball2'] = $code[1];
            $datamsg['ball3'] = $code[2];
            $datamsg['ball4'] = $code[3];
            $datamsg['ball5'] = $code[4];
            $datamsg['ball6'] = $code[5];
            $datamsg['ball7'] = $code[6];
            $datamsg['ball8'] = $code[7];
            $datamsg['ball9'] = $code[8];
            $datamsg['ball10'] = $code[9];
            if($datamsg['ball1']<6){
               $datamsg['page'] = '跌'; 
               $datamsg['status'] = 0; 
            }
            if($datamsg['ball1']>5){
               $datamsg['page'] = '涨'; 
               $datamsg['status'] = 1; 
            }
            // $msg = db('result')->field('qishu')->count();

            // if(!$msg){
            //     $datamsg['time'] = $datas[1]['dateline'];
            //     $id =db('result')->insertGetId($datamsg);
            //     $next = db('result')->where('id',$id)->find();
            //     $time = strtotime($next['time'])+300;
            //     $da['qishu'] = $next['qishu']+1;
            //     $da['time'] = date('Y-m-d H:i:s',$time);
            //     $ne = db('result')->insertGetId($da);
            // }

            $next_time = $this->data($datamsg['qishu']); 

            $qi = db('result')->where('qishu',$datamsg['qishu'])->find();
            if(empty($qi['ball1'])){
                    $qishu = db('result')->where('qishu',$datamsg['qishu'])->update($datamsg);
            }
            $code = controller('Orders', 'controller');
            $win = $code->wins(); 
            return $win; 
        }
    } 

    public function index2(){
         $data = db('result')->field('qishu,ball1')->limit(1,10)->order('id desc')->select();
         return json($data);
    } 

    //投资规则
    public function rule(){
        $open = db('set')->where('id',1)->find();
        $this->assign('set',$open['allodds']);
        return $this->fetch();
    }

    //下单成功
    public function successs(){
        return $this->fetch();
    }

    public function aa(){
       /* $user = controller('Orders', 'controller');
        $a =  $user->wins(); */
       /* $qi = db('result')->where('qishu',684565)->find();
        $time = strtotime($qi['time'])+3600;
        $time = date('Y-m-d',$time);
        dump($time);*/
     /*   $qi = db('result')->limit(1)->order('id desc')->select();
        $time = strtotime($qi[0]['time']);
        $times = date('Y-m-d H:i:',$time);
        $timea = db('set')->field('return')->where('id',1)->find();
        $timess = $times.$timea['return'];
        $up = db('result')->where('id',$qi[0]['id'])->setField('time',$timess);
        dump($up);*/
    }
}
